<template>
  <div class="content">
    <div class="card-box-content" v-if='projectInfo.stageStatus == 22'>
      <el-form label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="评审项目阶段" class="text--info subtext mg-item">
              {{processName(projectInfo.stageStatus)}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" class="text--info subtext mg-item">
              {{
                model.changeFormData.projectNameOld ||
                model.changeFormData.projectName
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" class="text--info subtext mg-item">
              {{ model.changeFormData.projectCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="终止原因" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationReason }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" class="text--info subtext mg-item">
              {{ model.changeFormData.createUserName }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="终止事由" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationReason }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请单位" class="text--info subtext mg-item">
              {{ model.changeFormData.ext2 }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="终止时间" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间" class="text--info subtext mg-item">
              {{ model.changeFormData.createTime }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="备注" class="text--info subtext mg-item">
              {{ model.changeFormData.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="card-box-content" v-else>
      <el-form label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="评审项目阶段" class="text--info subtext mg-item">
              {{processName(projectInfo.stageStatus)}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" class="text--info subtext mg-item">
              {{
                model.changeFormData.projectNameOld ||
                model.changeFormData.projectName
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" class="text--info subtext mg-item">
              {{ model.changeFormData.projectCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="变更原因" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationMatters }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" class="text--info subtext mg-item">
              {{ model.changeFormData.createUserName }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="变更事由" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationReason }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请单位" class="text--info subtext mg-item">
              {{ model.changeFormData.ext2 }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="变更时间" class="text--info subtext mg-item">
              {{ model.changeFormData.alterationTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间" class="text--info subtext mg-item">
              {{ model.changeFormData.createTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="备注" class="text--info subtext mg-item">
              {{ model.changeFormData.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="card-box form-box base-info" v-if="baseInfo != null">
      <p>基础信息相关变更</p>
      <div v-html="baseInfo" class="card-box-content"></div>
    </div>

    <div
      class="card-box form-box"
      v-if="model.projectTargetList.length > 0"
    >
      <p>项目目标及考核指标相关变更</p>
      <Goal :data="model.projectTargetList" :disabled="true" />
    </div>

    <div class="card-box form-box"  v-if="model.projectPlan && model.projectPlan.length">
      <p>项目计划相关变更</p>
      <Plan :data="model.projectPlan" :disabled="true" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import Goal from "/@/views/project/create/components/goal.vue";
import Plan from "/@/views/project/create/components/plan.vue";
import { ref, onMounted, reactive } from "vue";
import {useRoute } from "vue-router";
import {processStatus}  from  "/@/utils/globalConstant";
import { flowTypeObj } from "/@/components/approveDialog/type";
import { postAlterationInfo } from "/@/api/change";
import * as api from "/@/api/change";
defineProps({
  projectInfo: {
    type: Object
  },
});
const route = useRoute();
const nodeId = route.query.nodeId ? route.query.nodeId : "ACT_010";
let baseInfo = ref(null);
let contractInfo = ref(null);
const model = reactive({
  reason: "",
  flowType: "2001",
  approveHandlerId: null,
  isNoReject: false,
  dialogType: "approval",
  saveLoading: false,
  projectTargetList: [],
  projectPlan: [],
  changeFormData: {
    projectName: "",
    projectNameOld: "",
    projectCode: "",
    createTime: "",
    createUserName: "",
    ext2: "",
    alterationMatters: "",
    alterationReason: "",
    alterationTime: "",
    remark: "",
    applyStatus: "1"
  },
  changeRecordFileList: [],
  expertRecordFileList: [],
  otherFileList: []
});

const getDetail = async (id: string): Promise<any> => {
  try {
    const { code, data } = await postAlterationInfo({ id });
    if (code === 200) {
      if (data.alterJsonInfo) {
        const {
          alterBaseInfo,
          alterContractInfo,
          alterTargetInfo,
          alterPlanInfo
        } = JSON.parse(data.alterJsonInfo);

        baseInfo.value = alterBaseInfo || null;
        contractInfo.value = alterContractInfo;
        model.projectTargetList = alterTargetInfo || [];

        model.projectPlan =
          alterPlanInfo &&
          alterPlanInfo.map(el => {
            return {
              time:
                el.startTime.split(" ")[0] + "至" + el.endTime.split(" ")[0],
              timeObj: [el.startTime.split(" ")[0], el.endTime.split(" ")[0]],
              content: el.researchContent,
              goal: el.goal,
              alterationType: el.alterationType
            };
          });
      }
      model.changeFormData = data;

      model.flowType = data.approvalTypeCode;
      if (model.flowType) {
        model.isNoReject = flowTypeObj[model.flowType][nodeId].isNoReject; //不展示驳回按钮(包括驳回上一级和回退到指定节点)
      }
      if (data.alterationSignRecord) {
        model.changeRecordFileList = JSON.parse(data.alterationSignRecord);
        model.changeRecordFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
      if (data.expertSignRecord) {
        model.expertRecordFileList = JSON.parse(data.expertSignRecord);
        model.expertRecordFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
      if (data.otherFile) {
        model.otherFileList = JSON.parse(data.otherFile);
        model.otherFileList.map(el => {
          el["name"] = el.fileName;
        });
      }
    }
  } catch (err) {
    console.log(err);
  }
};

const getTableList = async (projectManageId) => {
  try {
    const { code, data } = await api.postAlterationList({
      alterNumLike: "", // 单据编号
      alterNameLike: "", // 变更名称
      projectManageId: projectManageId, // 项目名称
      createUserNameLike: "", // 申请人
      createTimeStrStart: "",
      createTimeStrEnd: "",
      pageNum: 1,
      pageSize: 0
    });
    if (code === 200) {
      const id = data?.list?data?.list[0]?.id:''
      getDetail(id)
    }
  } catch (err) {
    console.log(err);
  }
};

const processName = (stageStatus) => {
  return processStatus.get(+stageStatus)
}

onMounted(() => {
  if (route.query.projectManageId) {
    getTableList(route.query.projectManageId)
  }
});
</script>

<style lang="scss" scoped>
.content {
  :deep .el-page-header__content {
    font-size: 16px;
  }
  .card-box {
    margin-bottom: 20px;
    p{
      margin-bottom: 10px;
      font-size: 14px;
    }
    &--headerwrap {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    &--setbtn {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      width: 140px;
    }
    &-content {
      color: #606266;
      font-size: 14px;
    }
  }
  :deep .change-form {
    .el-form-item__content {
      display: flex;
      align-items: center;
      .record {
        display: flex;
        align-items: center;
        .el-upload-list__item:first-child {
          margin-top: 0;
        }
      }
    }
  }

  .header__wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.width-custom220 {
  width: 220px;
}

.form-info {
  margin-bottom: 20px;
}
.header-btns {
  text-align: right;
}
.mg-item{
  margin-bottom: 2px;
}
.base-info{
  margin-top:20px
}
</style>
